﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Celeriq.Common;

namespace Celeriq.ManagementStudio
{
	public partial class EditFieldForm : Form
	{
		private RepositoryDefinition _repository = null;
		private FieldDefinition _field = null;

		public EditFieldForm()
		{
			InitializeComponent();

			txtName.GotFocus += TextboxSelectOnEnter;
			txtName.Enter += TextboxSelectOnEnter;

			lblLengthDefined.Location = udLength.Location;
			lblLengthDefined.Size = udLength.Size;

			cboDataType.Items.Clear();

			//Data types
			var dataTypeList = Enum.GetNames(typeof(RepositoryDefinition.DataTypeConstants));
			foreach (var item in dataTypeList)
			{
				cboDataType.Items.Add(item.ToString());
			}

			//Dimension types
			var dimensionTypeList = Enum.GetNames(typeof(RepositoryDefinition.DimensionTypeConstants));
			foreach (var item in dimensionTypeList)
			{
				cboDimensionType.Items.Add(item.ToString());
			}

		}

		private void TextboxSelectOnEnter(object sender, EventArgs e)
		{
			((TextBox) sender).SelectAll();
		}

		public EditFieldForm(FieldDefinition field, RepositoryDefinition repository)
			: this()
		{
			_field = field;
			_repository = repository;

			txtName.Text = field.Name;
			chkAllowTextSearch.Checked = field.AllowTextSearch;
			cboDataType.SelectedItem = Enum.Parse(typeof(RepositoryDefinition.DataTypeConstants), field.DataType.ToString()).ToString();
			lblFieldType.Text = field.FieldType.ToString();

			var dimension = field as DimensionDefinition;
			if (dimension != null)
			{
				//Load all dimensions in the parent box
				cboParent.Items.Add("(No Parent)");
				cboParent.SelectedIndex = 0;
				cboParent.Items.AddRange(repository.FieldList.Where(x => x.FieldType == RepositoryDefinition.FieldTypeConstants.Dimension && x != field).Select(x => x.Name).ToArray());

				cboParent.SelectedItem = dimension.Parent;
				cboDimensionType.SelectedItem = Enum.Parse(typeof(RepositoryDefinition.DimensionTypeConstants), dimension.DimensionType.ToString()).ToString();
			}
			else
			{
				cboParent.Visible = false;
				lblParent.Visible = false;
			}
			this.EnableFields();

			chkPrimaryKey.Checked = field.IsPrimaryKey;
			udLength.Value = field.Length;
		}

		public FieldDefinition GetNewDefinition()
		{
			return null;
		}

		private void EnableFields()
		{
			var t = (RepositoryDefinition.FieldTypeConstants)Enum.Parse(typeof(RepositoryDefinition.FieldTypeConstants), lblFieldType.Text);
			if (t == RepositoryDefinition.FieldTypeConstants.Dimension)
			{
				lblDimensionType.Enabled = true;
				cboDimensionType.Enabled = true;
			}
			else
			{
				cboDimensionType.SelectedIndex = 0;
				lblDimensionType.Enabled = false;
				cboDimensionType.Enabled = false;
			}
		}

		private void cmdOK_Click(object sender, EventArgs e)
		{
			_field.Name = txtName.Text;
			_field.AllowTextSearch = chkAllowTextSearch.Checked;
			_field.DataType = (RepositoryDefinition.DataTypeConstants)Enum.Parse(typeof(RepositoryDefinition.DataTypeConstants), cboDataType.SelectedItem.ToString());
			//_field.FieldType = (RepositoryDefinition.FieldTypeConstants)Enum.Parse(typeof(RepositoryDefinition.FieldTypeConstants), cboFieldType.SelectedItem.ToString());

			var dimension = _field as DimensionDefinition;
			if (dimension != null)
			{
				dimension.DimensionType = (RepositoryDefinition.DimensionTypeConstants)Enum.Parse(typeof(RepositoryDefinition.DimensionTypeConstants), cboDimensionType.SelectedItem.ToString());
				if (cboParent.SelectedIndex == 0)
					dimension.Parent = null;
				else
					dimension.Parent = (string)cboParent.SelectedItem;
			}
			this.EnableFields();

			_field.IsPrimaryKey = chkPrimaryKey.Checked;
			_field.Length = (int)udLength.Value;

			this.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.Close();
		}

		private void cmdCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.Close();
		}

		private void cboFieldType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.EnableFields();
		}

		private void cboDataType_SelectedIndexChanged(object sender, EventArgs e)
		{
			RepositoryDefinition.DataTypeConstants a;
			if (Enum.TryParse<RepositoryDefinition.DataTypeConstants>(cboDataType.SelectedItem.ToString(), true, out a))
			{
				chkPrimaryKey.Enabled = (a == RepositoryDefinition.DataTypeConstants.Int);
				if (!chkPrimaryKey.Enabled) chkPrimaryKey.Checked = false;

				chkAllowTextSearch.Enabled = (a == RepositoryDefinition.DataTypeConstants.String);
				if (!chkAllowTextSearch.Enabled) chkAllowTextSearch.Checked = false;

				switch(a)
				{
					case RepositoryDefinition.DataTypeConstants.Bool:
						udLength.Visible = false;
						lblLengthDefined.Visible = true;
						break;
					case RepositoryDefinition.DataTypeConstants.DateTime:
						udLength.Visible = false;
						lblLengthDefined.Visible = true;
						break;
					case RepositoryDefinition.DataTypeConstants.Float:
						udLength.Visible = false;
						lblLengthDefined.Visible = true;
						break;
					case RepositoryDefinition.DataTypeConstants.GeoCode:
						udLength.Visible = false;
						lblLengthDefined.Visible = true;
						break;
					case RepositoryDefinition.DataTypeConstants.Int:
						udLength.Visible = false;
						lblLengthDefined.Visible = true;
						break;
					case RepositoryDefinition.DataTypeConstants.List:
						udLength.Visible = false;
						lblLengthDefined.Visible = true;
						break;
					case RepositoryDefinition.DataTypeConstants.String:
						udLength.Visible = true;
						lblLengthDefined.Visible = false;
						break;
				}
			}
		}

	}
}
